﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Zwms.Client.CommonUI;
using Zwms.Data.CommonObject;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.InfoWms
{
    public partial class UnitForm : BaseForm
    {
        #region 成员变量
        public BaseUnit CurrentRecord { get; set; }
        private EnumOpenType OpenType { get; set; }

        #endregion
        public UnitForm(EnumOpenType opentype, string sRecordCode)
        {
            InitializeComponent();
            OpenType = opentype;

            if (opentype == EnumOpenType.New)
            {
                CurrentRecord = new BaseUnit();
            }
            else if (opentype == EnumOpenType.Open)
            {
                tbUnitCode.ReadOnly = true;
                //通过服务读取'入库计划明细'的实例
                ServiceReferenceZwms.ServiceZwmsClient client =
                    new ServiceReferenceZwms.ServiceZwmsClient();
                BaseUnit record = new BaseUnit();
                record.Id = sRecordCode;
                CurrentRecord = client.QueryUnit(record);
            }
            InitBtnImage(); 
            InitControl();
        }

        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnClear.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
        }

        private void InitControl()
        {
            if (this.OpenType == EnumOpenType.New)
            {
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadInfo(CurrentRecord);
            }
        }

        private void LoadInfo(BaseUnit record)
        {
            this.tbUnitCode.Text = record.Id.Trim();
            this.tbUnitName.Text = record.UnitName.Trim();

            //------------------------------------------------------
           ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
            DateTime dtNow = client1.GetCurrentSystemTime();

            this.tbCreateUser.Text = record.CreateUserName;
            this.dpuCreateDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.CreateDate + record.CreateTime));
            this.tbEditUser.Text = record.EditUserName;
            this.dpuEditDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.EditDate + record.EditTime));
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.OpenType == EnumOpenType.New)
            {
                
                if (tbUnitCode.Text.Trim() == "")
                {
                    bRet = false;
                    tbUnitCode.BackColor = Color.Yellow;
                }
            }

            if (tbUnitName.Text.Trim() == "")
            {
                bRet = false;
                tbUnitName.BackColor = Color.Yellow;
            }

            if (!bRet)
            {
                return;
            }
            if (MessageBox.Show("您确定要添加（修改）计量单位信息吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                BaseUnit record = new BaseUnit();
                record.Id =tbUnitCode.Text;
                record.UnitName =tbUnitName.Text;
                //------------------------------------
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                if (this.OpenType == EnumOpenType.New)
                {
                    record.CreateUserName = MainForm.CurrentUser.UserName;
                    record.CreateUserCode = MainForm.CurrentUser.Id;
                    record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                    record.EditUserName = "";
                    record.EditUserCode = "";
                    record.EditDate = "";
                    record.EditTime = "";
                }
                else
                {
                    record.EditUserName = MainForm.CurrentUser.UserName;
                    record.EditUserCode = MainForm.CurrentUser.Id;
                    record.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
                }

                ServiceReferenceZwms.ServiceZwmsClient client =
                    new ServiceReferenceZwms.ServiceZwmsClient();
                if (this.OpenType == EnumOpenType.New)
                {
                    bRet = client.AddUnit(record);
                }
                else
                {
                    bRet = client.UpdateUnit(record);
                }
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }


            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.OpenType == EnumOpenType.New)
            {
                tbUnitCode.Text = "";
            }

            tbUnitName.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void Control_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender);
        }

        private void Control_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender);
        }
    }
}
